package thingtest.game;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Iterator;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JPanel;

public class GameScreen extends JPanel implements Observer {
	
	

	/**
	 * 
	 */
	private static final long serialVersionUID = -6282931337809628382L;
	private GameModel gameModel;
	
	GameScreen(){
		
		setPreferredSize(new Dimension(640,480));
		this.setBackground(Color.BLACK);
		this.gameModel = new GameModel();
		this.addKeyListener(new KeyboardControl(gameModel));
		this.addMouseMotionListener(new MouseControl(gameModel));
		this.addMouseListener(new MouseControl(gameModel));
		this.setFocusable(true);


		this.gameModel.getScenario().addObserver(this);
		this.gameModel.start();

		
	}

	@Override
	public void update(Observable arg0, Object arg1) {

		this.repaint();
		
	}
	
	@Override 
	public void paintComponent(Graphics g){
		
		super.paintComponent(g);
		Iterator<Thing> iterator = this.gameModel.listOfThings().iterator();
		g.setColor(Color.WHITE);
		while (iterator.hasNext()){
			Thing thing = iterator.next();
			double x = thing.location().x;
			double y = thing.location().y;
			double xFacing = thing.directionThisIsFacing().x;
			double yFacing = thing.directionThisIsFacing().y;
			g.drawLine((int)x, (int)y, (int)(xFacing + x), (int)(yFacing + y));
			g.draw3DRect((int)x, (int)y, (int)thing.radius(), (int)thing.radius(), false);
		}
		if (this.isFocusOwner()) g.drawChars("this thing has Focus".toCharArray(), 0, 20, 30, 30);
		g.drawChars((Integer.toString(gameModel.getScenario().containedThings().size()) + "       ").toCharArray(), 0, 5 , 40, 40);

		
		
	}

}
